package cn.itcast.mq.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.retry.RepublishMessageRecoverer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description RetryConfig
 * @Author junjie Wu 吴俊杰
 * @Date 2024-03-23
 */

/**
 * 消费者失败消息处理策略
 * 开启spring本地重试模式后。重试次数耗尽，如果消息依然失败，则需要有MessageRecoverer接口来处理
 * 它包含三种不同的实现
 * RejectAndDontRequeueRecoverer:重试耗尽后，直接reject，丢弃消息，默认就是这种方式
 * ImmediateRequeueMessageRecoverer:重试耗尽后，返回nack，消息重新入队
 * RepublishMessageRecoverer:重试耗尽后，将失败消息投递到指定的交换机（推荐使用）
 */
@Configuration
public class RetryConfig {

    @Bean
    public DirectExchange errorMessageExchange() {
        return new DirectExchange("error.direct");
    }

    @Bean
    public Queue errorQueue() {
        return new Queue("error.queue",true);
    }

    @Bean
    public Binding errorBinding(Queue errorQueue, DirectExchange errorMessageExchange){
        return BindingBuilder.bind(errorQueue).to(errorMessageExchange).with("error");
    }
    //
    @Bean
    public RepublishMessageRecoverer republishMessageRecoverer(RabbitTemplate rabbitTemplate) {
        return new RepublishMessageRecoverer(rabbitTemplate,"error.direct","error");
    }
}
